#pragma once


#include <IXGif.h>
#include <IXTimer.h>
#include "IXGifDraw.h"
#include "XFrameImpl.h"

template <class I>
class CXGifImpl : public CXFrameImpl<I>, public IXTimer
{
    typedef CXFrameImpl<I> superclass;

    BEGIN_DEF_SKIN_CLASS(CXGif, superclass, gs_strGuidXSkin)
        IMP_BASIC_INTF(IXGif,    0)
    END_DEF_SKIN_CLASS();

    IMPL_CONTROL_OBJ(CXGifImpl, superclass, _T("Gif"));

public:
    CXGifImpl();
    virtual ~CXGifImpl();

public:
    // IXGif
    virtual HRESULT STDMETHODCALLTYPE   put_Gif(IN LPCTSTR szXNC);
    virtual HRESULT STDMETHODCALLTYPE   get_ImagePath(OUT BSTR* pbstrXNC);

    virtual HRESULT STDMETHODCALLTYPE   Start();
    virtual HRESULT STDMETHODCALLTYPE   Stop();

    virtual void    STDMETHODCALLTYPE   SetPlayMode(GifPlayMode eMode);
    virtual GifPlayMode STDMETHODCALLTYPE GetPlayMode();

    virtual void    STDMETHODCALLTYPE   SetFixedInterval(int nFrameDelayValue);
    virtual int     STDMETHODCALLTYPE   GetFixedInterval();

    virtual DWORD   STDMETHODCALLTYPE   GetFrameCount();

public:
    // IXFrame
    virtual void    STDMETHODCALLTYPE   GetAutoSize(OUT LPSIZE pAutoSize);
    virtual void    STDMETHODCALLTYPE   Draw(XCANVAS xCanvas, const RECT& rcDraw);

public:
    // IXBaseObj
    virtual void    STDMETHODCALLTYPE   OnFinalRelease();

public:
    // IXTimer
    virtual void    STDMETHODCALLTYPE   OnTimer(UINT_PTR uTimerId, void* pData);

protected:
    // CXControlImpl
    virtual void OnCreate();

protected:
    void        ReleaseData();

    __inline DWORD GetNextElapse()
    {
        DWORD dwTimerElapse = 0;

        if(m_ePlayMode == GPM_Default)
            dwTimerElapse = m_pGifDraw->GetDelayValue();
        else if(m_ePlayMode == GPM_Fixed)
            dwTimerElapse = m_nFrameDelayValue;

        const DWORD g_dwMinDelayValue = 50;
        if(dwTimerElapse < g_dwMinDelayValue)
            dwTimerElapse = g_dwMinDelayValue;

        return dwTimerElapse;
    }

protected:
    XString     m_strGifXNC;
    IXGifDraw*  m_pGifDraw;
    UINT_PTR    m_uTimerId;

    GifPlayMode m_ePlayMode;
    int         m_nFrameDelayValue;
};


class CXGif : public CXGifImpl<IXGif>
{
};
